#!/usr/bin/env python
#--coding:utf-8--
import sys
import os

sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index,and_,func
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy import create_engine
from sqlalchemy.orm.exc import MultipleResultsFound

import hashlib

engine = create_engine("mysql+pymysql://root:123456@10.0.0.20:3306/hostmessage?charset=utf8",encoding="utf-8",echo=True, max_overflow=5)

Base = declarative_base()

class Users(Base):
    __tablename__="users"
    userid=Column(Integer,primary_key=True,autoincrement=True)
    username=Column(String(50),unique=True)
    password=Column(String(15))
    def __repr__(self):
        return "<Users(username='%s',password='%s')>"%(self.username,self.password)
#Base.metadata.create_all(engine)
Session=sessionmaker(bind=engine)
session=Session()


#u1=Users(username="alex",password="123456")
#u2=Users(username="jack",password="asdasd")

#session.add_all([u1,u2])
'''
for row in session.query(Users).all()[1:3]:
    print(row.username,row.userid)'''
try:
    print(session.query(Users).order_by(Users.userid).scalar())
except MultipleResultsFound as e:
    print(e)
session.commit()



